﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Data;

using System.Threading;


namespace App1
{
    class SocKet
    {
        private const int BUFFER_SIZE = 1024;
        private const int PORT_NUMBER = 2008;

       

        static ASCIIEncoding encoding = new ASCIIEncoding();

        public string IP;
        TcpClient client = new TcpClient();
        Stream stream;// = client.GetStream();


        public bool connect_Server(string ip)// connect den server co dia chi ip
        {
            try
            {
                IP = ip;
                // 1. connect
                client.Connect(ip, PORT_NUMBER);
                stream = client.GetStream();


                return true;
            }
            catch (Exception ex)
            {
                return false;
                // Console.WriteLine("Error: " + ex);
            }
        }

        public void Send_Server(string Key)// gui key wa server
        {
            //var reader=new StreamReader(stream);
            var writer = new StreamWriter(stream);
            writer.AutoFlush = true;
            // 2. send
            writer.WriteLine(Key);
        }

        public string Receive_Server()// nhan thong diep tu sever
        {
            var reader = new StreamReader(stream);
            // var writer = new StreamWriter(stream);
            string str;
            // recevie
            str = reader.ReadLine();
            return str;
        }

        public void DisConnect_Server()
        {
            stream.Close();
            client.Close();
        }

    }
}
